package com.bignerdranch.android.ecar.Adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.bignerdranch.android.ecar.R;
import com.bignerdranch.android.ecar.Utils.CircleNetworkImage;
import com.bignerdranch.android.ecar.Utils.MyImageCache;
import com.bignerdranch.android.ecar.Utils.RequestUtil;
import com.bignerdranch.android.ecar.Utils.URLBank;
import com.jaeger.ninegridimageview.NineGridImageView;
import com.jaeger.ninegridimageview.NineGridImageViewAdapter;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/8/3.
 */
public class BlogAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater mInflater;
    private JSONArray mData;
    ImageLoader loader;
    private List<String> imgs;

    public BlogAdapter(Context context, JSONArray data) {
        mContext = context;
        mData = data;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mData.length();
    }

    @Override
    public Object getItem(int position) {
        try {
            return mData.get(position);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = new ViewHolder();
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_blog, null);
        }

        if (convertView.getTag()== null){
            holder.iv_user = (CircleNetworkImage) convertView.findViewById(R.id.iv_user);
            holder.tv_user = (TextView) convertView.findViewById(R.id.tv_user);
            holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
            holder.tv_content = (TextView) convertView.findViewById(R.id.tv_content);
            holder.iv_image = (NineGridImageView) convertView.findViewById(R.id.iv_image);
            holder.tv_likecount = (TextView) convertView.findViewById(R.id.tv_likecount);
            holder.tv_count = (TextView) convertView.findViewById(R.id.tv_count);

            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }

        try {
            String img_name = mData.getJSONObject(position).getString("headPic");
            String headurl = URLBank.teacher_url+URLBank.upload_url+img_name;
            loader = new ImageLoader(RequestUtil.getRequestQueue(mContext), new MyImageCache());
            holder.iv_user.setDefaultImageResId(R.drawable.sample);
            holder.iv_user.setErrorImageResId(R.drawable.sample_failed);
            holder.iv_user.setImageUrl(headurl, loader);

            holder.iv_image.setSingleImgSize(1000);

            NineGridImageViewAdapter adapter = new NineGridImageViewAdapter() {
                @Override
                protected void onDisplayImage(Context context, ImageView imageView, Object o) {
                    MyImageCache lruImageCache = new MyImageCache();
                    ImageLoader imageLoader = new ImageLoader(RequestUtil.getRequestQueue(mContext), lruImageCache);
                    ((NetworkImageView) imageView).setImageUrl((String) o, imageLoader);

                }

                @Override
                protected ImageView generateImageView(Context context) {
                    NetworkImageView networkImageView = new NetworkImageView(context);
                    networkImageView.setScaleType(ImageView.ScaleType.FIT_XY);
                    networkImageView.setDefaultImageResId(R.drawable.sample);
                    networkImageView.setErrorImageResId(R.drawable.sample_failed);
                    return networkImageView;
                }

                @Override
                protected void onItemImageClick(Context context, int index, List list) {
                    super.onItemImageClick(context, index, list);
                }
            };
            holder.iv_image.setAdapter(adapter);
            String string_imgs = mData.getJSONObject(position).getString("imgs");
            Log.e("------",string_imgs);
            String[] urlarray = string_imgs.split(",");
            imgs = new ArrayList<>();
            for (int i = 0; i < urlarray.length; i++) {
                imgs.add(URLBank.teacher_url+URLBank.upload_url + urlarray[i]);
                Log.e("",URLBank.teacher_url+URLBank.upload_url + urlarray[i]);
            }
            holder.iv_image.setImagesData(imgs);
            holder.tv_user.setText(mData.getJSONObject(position).getString("username"));
            holder.tv_time.setText(mData.getJSONObject(position).getString("createDate"));
            holder.tv_content.setText(mData.getJSONObject(position).getString("content"));
            holder.tv_user.setText(mData.getJSONObject(position).getString("username"));


        } catch (JSONException e) {
            e.printStackTrace();
        }
        return convertView;
    }


    public static class ViewHolder {
        public CircleNetworkImage iv_user;
        public TextView tv_user;
        public TextView tv_time;
        public TextView tv_content;
        public NineGridImageView iv_image;
        public TextView tv_likecount;
        public TextView tv_count;

    }
}
